package com.java110.mall.shop.bmo.shopAudit.impl;

import com.java110.dto.area.AreaDto;
import com.java110.intf.user.IAreaInnerServiceSMO;
import com.java110.mall.shop.bmo.shopAudit.IGetShopAuditBMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.java110.intf.shop.IShopAuditInnerServiceSMO;
import com.java110.dto.shop.ShopAuditDto;
import org.springframework.http.HttpStatus;

import java.util.ArrayList;
import java.util.List;

@Service("getShopAuditBMOImpl")
public class GetShopAuditBMOImpl implements IGetShopAuditBMO {

    @Autowired
    private IShopAuditInnerServiceSMO shopAuditInnerServiceSMOImpl;

    @Autowired
    private IAreaInnerServiceSMO areaInnerServiceSMOImpl;
    /**
     *
     *
     * @param  shopAuditDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(ShopAuditDto shopAuditDto) {


        int count = shopAuditInnerServiceSMOImpl.queryShopAuditsCount(shopAuditDto);

        List<ShopAuditDto> shopAuditDtos = null;
        if (count > 0) {
            shopAuditDtos = shopAuditInnerServiceSMOImpl.queryShopAudits(shopAuditDto);
            freshShopImg(shopAuditDtos);
        } else {
            shopAuditDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) shopAuditDto.getRow()), count, shopAuditDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }


    /**
     * @param shopDtos
     */
    private void freshShopImg(List<ShopAuditDto> shopDtos) {
        String imgUrl = MappingCache.getValue( MappingConstant.FILE_DOMAIN,"IMG_PATH" );
        imgUrl += (!StringUtil.isEmpty( imgUrl ) && imgUrl.endsWith( "/" ) ? "" : "/");
        for (ShopAuditDto shopDto : shopDtos) {
            if(StringUtil.isEmpty(shopDto.getShopLogo()) || !shopDto.getShopLogo().startsWith("http")) {
                shopDto.setShopLogo(imgUrl + shopDto.getShopLogo());
            }
            if (!StringUtil.isEmpty( shopDto.getAreaCode() )) {
                AreaDto areaDto = new AreaDto();
                areaDto.setAreaCode( shopDto.getAreaCode() );
                List<AreaDto> areaDtos = areaInnerServiceSMOImpl.getProvCityArea( areaDto );
                if (areaDtos != null && areaDtos.size() > 0) {
                    shopDto.setAreaName( areaDtos.get( 0 ).getProvName() + "-" + areaDtos.get( 0 ).getCityName() + "-" + areaDtos.get( 0 ).getAreaName() );
                }
            }
        }
    }
}
